package senior.StringTest;

/**
 * 传闻StringBuilder比StringBuffer快,
 * 比较一下，输出你的比较结果（拼接10万个相同的字符串，比较时间）。
 *
 * 测试结果:StringBuffer比StringBuilder快 5~8ms
 * @author shkstart
 * @creat 2022-04-21:39
 */
public class StringTest4 {
    public static void main(String[] args) {
        StringBuffer buffer = new StringBuffer();
        StringBuilder builder = new StringBuilder();
        long bufferStartTime = System.currentTimeMillis();
        for(int i = 0;i < 100000;i++){
            buffer.append("$");
        }
        long bufferEndTime = System.currentTimeMillis();

        long builderStartTime = System.currentTimeMillis();
        for(int i = 0;i < 100000;i++){
            builder.append("$");
        }
        long builderEndTime = System.currentTimeMillis();

        long time = (bufferEndTime - bufferStartTime) -(builderEndTime - builderStartTime);
        if(time > 0){
            System.out.println("拼接10万个相同的字符串,StringBuffer比StringBuilder快" + time + "ms");
        }else {
            System.out.println("拼接10万个相同的字符串,StringBuffer比StringBuilder慢" + -time + "ms");

        }
    }
}
